package com.gec.it.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.it.entity.Doctors;
import com.gec.it.service.DoctorsService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyUserRealm extends AuthorizingRealm {

    private DoctorsService service;

    public void setService(DoctorsService service) {
        this.service = service;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Doctors doc=(Doctors) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.addRoles(service.findRoles(doc.getDocId()));
        info.addStringPermissions(service.findPmses(doc.getDocId()));
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String uname = (String) authenticationToken.getPrincipal();
        QueryWrapper<Doctors> qw=new QueryWrapper<>();
        qw.lambda().eq(Doctors::getDocName,uname);
        Doctors doc=service.getOne2(qw);
        if(doc!=null){
            return new SimpleAuthenticationInfo(doc,doc.getDocPwd(),this.getName());
        }
        throw new UnknownAccountException("无此用户");
    }

}
